package com.ruoyi.repair.service.impl;

import java.util.List;
import java.util.UUID;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.equipment.domain.CampusEquipment;
import com.ruoyi.equipment.mapper.CampusEquipmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.repair.mapper.EquipmentRepairMapper;
import com.ruoyi.repair.domain.EquipmentRepair;
import com.ruoyi.repair.service.IEquipmentRepairService;

/**
 * 设备维修记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-03
 */
@Service
public class EquipmentRepairServiceImpl implements IEquipmentRepairService 
{
    @Autowired
    private EquipmentRepairMapper equipmentRepairMapper;
    @Autowired
    private CampusEquipmentMapper campusEquipmentMapper;

    /**
     * 查询设备维修记录
     * 
     * @param repairId 设备维修记录主键
     * @return 设备维修记录
     */
    @Override
    public EquipmentRepair selectEquipmentRepairByRepairId(String repairId)
    {
        return equipmentRepairMapper.selectEquipmentRepairByRepairId(repairId);
    }

    /**
     * 查询设备维修记录列表
     * 
     * @param equipmentRepair 设备维修记录
     * @return 设备维修记录
     */
    @Override
    public List<EquipmentRepair> selectEquipmentRepairList(EquipmentRepair equipmentRepair)
    {
        return equipmentRepairMapper.selectEquipmentRepairList(equipmentRepair);
    }

    /**
     * 新增设备维修记录
     * 
     * @param equipmentRepair 设备维修记录
     * @return 结果
     */
    @Override
    public int insertEquipmentRepair(EquipmentRepair equipmentRepair)
    {
        // 修改设备状态为2，维修
        CampusEquipment campusEquipment = new CampusEquipment();
        CampusEquipment campusEquipment1 = campusEquipmentMapper.selectCampusEquipmentByEquipmentId(equipmentRepair.getEquipmentId());
        if(campusEquipment1.getStatus().equals("2")){
            throw new ServiceException("设备已在维修");
        }
        if(campusEquipment1.getStatus().equals("3")){
            throw new ServiceException("设备已报废");
        }
        campusEquipment.setEquipmentId(equipmentRepair.getEquipmentId());
        campusEquipment.setStatus("2");
        equipmentRepair.setStartTime(DateUtils.getNowDate());
        equipmentRepair.setRepairStatus("1");
        equipmentRepair.setRepairId(UUID.randomUUID().toString().replace("-", "").substring(0, 8));
        campusEquipmentMapper.updateCampusEquipment(campusEquipment);
        return equipmentRepairMapper.insertEquipmentRepair(equipmentRepair);
    }

    /**
     * 修改设备维修记录
     * 
     * @param equipmentRepair 设备维修记录
     * @return 结果
     */
    @Override
    public int updateEquipmentRepair(EquipmentRepair equipmentRepair)
    {
        return equipmentRepairMapper.updateEquipmentRepair(equipmentRepair);
    }

    /**
     * 批量删除设备维修记录
     * 
     * @param repairIds 需要删除的设备维修记录主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentRepairByRepairIds(String[] repairIds)
    {
        return equipmentRepairMapper.deleteEquipmentRepairByRepairIds(repairIds);
    }

    /**
     * 删除设备维修记录信息
     * 
     * @param repairId 设备维修记录主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentRepairByRepairId(String repairId)
    {
        return equipmentRepairMapper.deleteEquipmentRepairByRepairId(repairId);
    }

    // 维修设备
    @Override
    public int repairEquipment(EquipmentRepair equipmentRepair){
        CampusEquipment campusEquipment = new CampusEquipment();
        // 恢复设备状态
        campusEquipment.setStatus("1");
        campusEquipment.setEquipmentId(equipmentRepair.getEquipmentId());
        campusEquipmentMapper.updateCampusEquipment(campusEquipment);
        return equipmentRepairMapper.updateEquipmentRepair(equipmentRepair);
    }
}
